package com.hnxxxy.workshop.controller;



import com.hnxxxy.workshop.request.FactoryPageRequest;
import com.hnxxxy.workshop.request.FactoryRequest;
import com.hnxxxy.workshop.service.FactoryService;
import com.hnxxxy.workshop.utils.R;
import com.hnxxxy.workshop.vo.FactoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工厂 前端控制器
 * </p>
 *
 * @author zqh
 * @since 2022-05-09
 */
@RestController
@RequestMapping("/factory")
@Api(value = "FactoryController", tags = {"工厂模块"})
public class FactoryController {

    @Resource
    private FactoryService factoryService;

    @PostMapping("/add")
    @ApiOperation(value = "创建工厂")
    public R add(@ApiParam("工厂对象") @RequestBody FactoryRequest factoryRequest){
        factoryService.add(factoryRequest);
        return R.buildSuccess();
    }

    @GetMapping("/remove/{id}")
    @ApiOperation(value = "删除工厂")
    public R deleteById(@ApiParam(value = "工厂id",required = true)
                        @PathVariable("id") int id){
        factoryService.deleteById(id);
        return R.buildSuccess();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改工厂")
    public R update( @ApiParam("工厂对象")
                     @RequestBody FactoryRequest factoryRequest
                   ){
        factoryService.update(factoryRequest);
        return R.buildSuccess();
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询全部工厂")
    public R findAllFactory(){
        List<FactoryVO> factoryVO = factoryService.findAllFactory();
        return R.buildSuccess(factoryVO);
    }

    @GetMapping("/findById/{id}")
    @ApiOperation(value = "查询全部工厂")
    public R findByIdFactory(@ApiParam(value = "工厂id",required = true)
                             @PathVariable("id") int id ){
        FactoryVO factoryVO = factoryService.findByIdFactory(id);
        return R.buildSuccess(factoryVO);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询全部工厂")
    public R findByIdFactory(@ApiParam(value = "分页对象",required = true)
                                 @RequestBody FactoryPageRequest factoryPageRequest){
        Map<String,Object> pageResult = factoryService.page(factoryPageRequest);
        return R.buildSuccess(pageResult);
    }

}

